<script lang="ts" setup name="模型转换">
import { onMounted, reactive, ref } from 'vue'
import DownEditModal from './modeFiledown.vue'
import { downloadConvertFile } from '@/api/system/convert'

import { colChange, currentHook, handleSelectionChange } from './down-hook'
import { getConvertList } from '@/api/system/convert'
import { convertPredict } from '@/api/system/predict'

//导入当前页面封装方法
import RightToolBar from '@/components/RightToolBar.vue'
import ColumnFilter from '@/components/ColumnFilter.vue'
import { i18n } from '@/lang'
const { t } = i18n.global

///导入当前页面封装方法
import { resetData, spliceMinioUrl } from '@/hooks/use-common'

/*查询模块*/
const queryParams: any = reactive({
  pageNum: 1,
  pageSize: 10,
  name: '', //平台名字
  in_file: '', //文件名
  out_file: '', //原名
  from_fmt: '', //文件后缀名
  to_fmt: '', //文件后缀名
  create_at: '' //文件后缀名
})

const tableHeadColumns = ref([
  { prop: 'id', label: t('modelConvert.id'), minWidth: 120, isTemplate: false, align: 'center', showColumn: true },
  {
    prop: 'user_id',
    label: t('modelConvert.user_id'),
    minWidth: 120,
    isTemplate: false,
    align: 'center',
    showColumn: true
  },
  {
    prop: 'in_file',
    label: t('modelConvert.in_file'),
    minWidth: 150,
    isTemplate: false,
    align: 'center',
    showColumn: true
  },
  {
    prop: 'from_fmt',
    label: t('modelConvert.from_fmt'),
    minWidth: 120,
    isTemplate: false,
    align: 'center',
    showColumn: true
  },
  {
    prop: 'out_file',
    label: t('modelConvert.out_file'),
    minWidth: 150,
    isTemplate: false,
    align: 'center',
    showColumn: true
  },
  {
    prop: 'to_fmt',
    label: t('modelConvert.to_fmt'),
    minWidth: 120,
    isTemplate: false,
    align: 'center',
    showColumn: true
  },
  {
    prop: 'create_at',
    label: t('modelConvert.create_at'),
    minWidth: 150,
    isTemplate: false,
    align: 'center',
    showColumn: true
  }
])

/** 上传文件 */
const handleFile = (row) => {
  refDownEditModal.value.showModal(row)
}

//备份数据
const bakQueryParams = JSON.stringify(queryParams)

//查询
const handleQuery = () => {
  queryParams.pageNum = 1
  getTableData()
}
//重置
const resetQuery = () => {
  resetData(queryParams, bakQueryParams)
  handleQuery()
}

const checkFileSuffix = (fileSuffix) => {
  const arr = ['.obj', '.stl', '.ply']
  return arr.some((type) => {
    return fileSuffix.includes(type)
  })
}

const getTableData = () => {
  loading.value = true
  const skip = (queryParams.pageNum - 1) * queryParams.pageSize

  getConvertList({
    page: skip,
    size: queryParams.pageSize
  }).then((res) => {
    loading.value = false
    ossList.value = res.data || []
    totalNum.value = res.total
  })
}

//新增模型预测
const addModelPredict = (row) => {
  convertPredict(row.id).then((res) => {})
}

const downloadFileItem = async (row) => {
  downloadConvertFile(row.id).then((res: any) => {
    const blob = new Blob([res], { type: 'text/plain' })
    const filename = row.out_file
    const url = URL.createObjectURL(blob)
    // 创建一个a标签用于下载文件
    const downloadLink = document.createElement('a')
    downloadLink.href = url
    downloadLink.download = filename // 指定下载文件名
    // 触发下载
    document.body.appendChild(downloadLink)
    downloadLink.click()
    // 清理并移除元素和对象URL
    document.body.removeChild(downloadLink)
    URL.revokeObjectURL(url)
  })
}

onMounted(() => handleQuery())

const { refDownEditModal, refElTable, totalNum, loading, ossList, showSearch } = currentHook(queryParams, getTableData)
</script>

<template>
  <div class="p-10px">
    <!-- <el-form v-show="showSearch" :model="queryParams" :inline="true" label-width="80px">
      <el-form-item label="文件名" prop="fileName">
        <el-input
          v-model.trim="queryParams.fileName"
          placeholder="请输入文件名"
          clearable
          class="wi-150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原名" prop="originalName">
        <el-input
          v-model.trim="queryParams.originalName"
          placeholder="请输入原名"
          clearable
          class="wi-150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件后缀名" prop="fileSuffix">
        <el-input
          v-model.trim="queryParams.fileSuffix"
          placeholder="请输入文件后缀名"
          clearable
          class="wi-150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="下载时间" style="width: 150px}">
        <el-date-picker
          v-model="timeRange"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <el-row :gutter="10" class="mb-10px">
      <el-button type="primary" plain @click="handleFile">{{ t('modelConvert.uploadFile') }}</el-button>

      <RightToolBar v-model:showSearch="showSearch" @queryTable="getTableData" />
      <ColumnFilter v-if="ossList.length" :is-operation="true" :cols="tableHeadColumns" @colChange="colChange" />
    </el-row>
    <el-table
      ref="refElTable"
      v-loading="loading"
      height="calc(100vh - 350px)"
      border
      :data="ossList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <!--column头字段-->
      <template v-for="item in tableHeadColumns">
        <el-table-column
          v-if="!item.isTemplate && item.showColumn"
          :key="item.prop"
          show-overflow-tooltip
          v-bind="item"
          :align="item.align || 'center'"
          :prop="item.prop"
          :label="item.label"
        />
      </template>

      <el-table-column
        :label="t('modelConvert.action')"
        align="center"
        width="80"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-tooltip :content="t('downModel.downText')" placement="top">
            <el-button link type="primary" icon="Download" size="large" @click="downloadFileItem(row)" />
          </el-tooltip>
          <el-tooltip :content="t('modelConvert.transformation_prediction')" placement="top">
            <el-button link type="primary" icon="Connection" size="large" @click="addModelPredict(row)" />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="columnSE">
      <Pagination
        v-show="totalNum > queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        :total="totalNum"
        @pagination="getTableData"
      />
    </div>
    <DownEditModal ref="refDownEditModal" @getTableData="getTableData" />
  </div>
</template>
